package stardust.product.platform.webc.spi.servlet.support;


import java.util.Iterator;
import stardust.product.platform.webc.spi.servlet.ServletWebcRequestHandler;


public class PageUtils
{
  
  private ServletWebcRequestHandler request;
  
  public PageUtils(ServletWebcRequestHandler request)
  {
    this.request = request;
  }

  public String getParameter()
  {
    StringBuffer buffer = new StringBuffer();
    Iterator<?> keys;
    String key;
    String[] value;
    
    keys = this.request.getRequest().getParameterMap().keySet().iterator();
    while (keys.hasNext())
    {
      key = (String) keys.next();
      
      if (key.equalsIgnoreCase("page"))
      {
        continue;
      }
      
      value = this.request.getRequest().getParameterValues(key);
      
      for (String v : value)
      {
        buffer.append("&").append(key).append("=").append(v);
      }
    }
    
//    return buffer.length() > 0 ? buffer.toString() : "";
    return buffer.toString();
  }
  
  public static class PageRange
  {
    
    public int begin;
    
    public int end;
    
    public int pageIndex;
    
    public int maxPageCount;
    
    public int maxRowCount;

    public int getBegin()
    {
      return begin;
    }

    public int getEnd()
    {
      return end;
    }

    public int getPageIndex()
    {
      return pageIndex;
    }

    public int getMaxPageCount()
    {
      return maxPageCount;
    }

    public int getMaxRowCount()
    {
      return maxRowCount;
    }
    
  }
  
  public static PageRange getPageRange(int pageIndex, int maxPageCount, int maxRowCount)
  {
    int LEN;
    PageRange pageRange = new PageRange();
    
    LEN = 5;
//    LEN = pageIndex + LEN > maxPageCount ? LEN + (LEN - (pageIndex + LEN - maxPageCount)) : LEN;
//    pageRange.begin = pageIndex - LEN <= 0 ? 1 : pageIndex - LEN;
//    
//    LEN = 5;
//    LEN = pageIndex - LEN <= 0 ? LEN + Math.abs(pageIndex - LEN) : LEN;
//    pageRange.end = pageIndex + LEN > maxPageCount ? maxPageCount : pageIndex + LEN;
    pageRange.begin = pageIndex - LEN <= 0 ? 1 : pageIndex - LEN;
    pageRange.end = pageIndex + LEN > maxPageCount ? maxPageCount : pageIndex + LEN;
    
    pageRange.pageIndex = pageIndex;
    pageRange.maxPageCount = maxPageCount;
    pageRange.maxRowCount = maxRowCount;
    
    return pageRange;
  }

}
